{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ceabf1eb-ca96-4791-90ad-e9acb31edf5c",
   "metadata": {},
   "source": [
    "# Text Embeddings Inference\n",
    "\n",
    ">[Hugging Face Text Embeddings Inference (TEI)](https://huggingface.co/docs/text-embeddings-inference/index) is a toolkit for deploying and serving open-source\n",
    "> text embeddings and sequence classification models. `TEI` enables high-performance extraction for the most popular models,\n",
    ">including `FlagEmbedding`, `Ember`, `GTE` and `E5`.\n",
    "\n",
    "To use it within langchain, first install `huggingface-hub`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "579f0677-aa06-4ad8-a816-3520c8d6923c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "%pip install --upgrade huggingface-hub"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c6b1015-bc3f-4283-93d5-11387be1b98d",
   "metadata": {},
   "source": [
    "Then expose an embedding model using TEI. For instance, using Docker, you can serve `BAAI/bge-large-en-v1.5` as follows:\n",
    "\n",
    "```bash\n",
    "model=BAAI/bge-large-en-v1.5\n",
    "revision=refs/pr/5\n",
    "volume=$PWD/data # share a volume with the Docker container to avoid downloading weights every run\n",
    "\n",
    "docker run --gpus all -p 8080:80 -v $volume:/data --pull always ghcr.io/huggingface/text-embeddings-inference:0.6 --model-id $model --revision $revision\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48eebefc-a631-48dd-9bde-4a987f81aa20",
   "metadata": {},
   "source": [
    "Finally, instantiate the client and embed your texts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "22b09777-5ba3-4fbe-81cf-a702a55df9c4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f9a92970-16f4-458c-b186-2a83e9f7d840",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "embeddings = HuggingFaceEndpointEmbeddings(model=\"http://localhost:8080\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "42105438-9fee-460a-9c52-b7c595722758",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "text = \"What is deep learning?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "20167762-0988-4205-bbd4-1f20fd9dd247",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.018113142, 0.00302585, -0.049911194]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_result = embeddings.embed_query(text)\n",
    "query_result[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "54b87cf6-86ad-46f5-b2cd-17eb43cb4d0b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "doc_result = embeddings.embed_documents([text])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6fba8be9-fabf-4972-8334-aa56ed9893e1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.018113142, 0.00302585, -0.049911194]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "doc_result[0][:3]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
